﻿using FastCloud.Base.Desgin;
using FastCloud.DMP.Entity;
using FastIOC.Attribute;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.DMP.DB
{
    [Component]
    public class DB_DMP
    {
        [AutoWired]
        private CommonDTO DTO;
        [AutoWired]
        private DB_Project DB_Project;
        [AutoWired]
        private DB_Table DB_Table;
        [AutoWired]
        private DB_Column DB_Column;

        public void InitTableDefaultStruct(string TableGuid,string TablePhysicalName)
        {
            //创建表，默认字段
            string TableDDLSQL = string.Format("CREATE TABLE {0} (RowGuid VARCHAR(100), UserGuid VARCHAR(100),UserName VARCHAR(100),OUGuid VARCHAR(100),OUName VARCHAR(100),PVIGuid VARCHAR(100),AddDate datetime, SortNum int,PRIMARY KEY (`RowGuid`))", TablePhysicalName);
            DTO.ExecuteSQL(TableDDLSQL);
            //同步表的默认字段到Frame_Column表
            Frame_Column Column = new Frame_Column();
            Column.TableGuid = TableGuid;
            Column.ColumnName = "RowGuid";
            Column.PhysicalColumnName = "RowGuid";
            Column.ColumnType = (int)Base.Enum.DMP.ColumnType.字符串;
            Column.ColumnLenght = 100;
            Column.BindControl = (int)Base.Enum.DMP.ControlType.文本框;
            Column.SortNum = 999;
            Column.SystemColumn = (int)Base.Enum.DMP.SystemColumn.系统字段;
            DB_Column.InsertOnlyInfo(Column);
            Column = new Frame_Column();
            Column.TableGuid = TableGuid;
            Column.ColumnName = "UserGuid";
            Column.PhysicalColumnName = "UserGuid";
            Column.ColumnType = (int)Base.Enum.DMP.ColumnType.字符串;
            Column.ColumnLenght = 100;
            Column.BindControl = (int)Base.Enum.DMP.ControlType.文本框;
            Column.SortNum = 999;
            Column.SystemColumn = (int)Base.Enum.DMP.SystemColumn.系统字段;
            DB_Column.InsertOnlyInfo(Column);
            Column = new Frame_Column();
            Column.TableGuid = TableGuid;
            Column.ColumnName = "UserName";
            Column.PhysicalColumnName = "UserName";
            Column.ColumnType = (int)Base.Enum.DMP.ColumnType.字符串;
            Column.ColumnLenght = 100;
            Column.BindControl = (int)Base.Enum.DMP.ControlType.文本框;
            Column.SortNum = 999;
            Column.SystemColumn = (int)Base.Enum.DMP.SystemColumn.系统字段;
            DB_Column.InsertOnlyInfo(Column);
            Column = new Frame_Column();
            Column.TableGuid = TableGuid;
            Column.ColumnName = "OUGuid";
            Column.PhysicalColumnName = "OUGuid";
            Column.ColumnType = (int)Base.Enum.DMP.ColumnType.字符串;
            Column.ColumnLenght = 100;
            Column.BindControl = (int)Base.Enum.DMP.ControlType.文本框;
            Column.SortNum = 999;
            Column.SystemColumn = (int)Base.Enum.DMP.SystemColumn.系统字段;
            DB_Column.InsertOnlyInfo(Column);
            Column = new Frame_Column();
            Column.TableGuid = TableGuid;
            Column.ColumnName = "OUName";
            Column.PhysicalColumnName = "OUName";
            Column.ColumnType = (int)Base.Enum.DMP.ColumnType.字符串;
            Column.ColumnLenght = 100;
            Column.BindControl = (int)Base.Enum.DMP.ControlType.文本框;
            Column.SortNum = 999;
            Column.SystemColumn = (int)Base.Enum.DMP.SystemColumn.系统字段;
            DB_Column.InsertOnlyInfo(Column);
            Column = new Frame_Column();
            Column.TableGuid = TableGuid;
            Column.ColumnName = "PVIGuid";
            Column.PhysicalColumnName = "PVIGuid";
            Column.ColumnType = (int)Base.Enum.DMP.ColumnType.字符串;
            Column.ColumnLenght = 100;
            Column.BindControl = (int)Base.Enum.DMP.ControlType.文本框;
            Column.SortNum = 999;
            Column.SystemColumn = (int)Base.Enum.DMP.SystemColumn.系统字段;
            DB_Column.InsertOnlyInfo(Column);
            Column = new Frame_Column();
            Column.TableGuid = TableGuid;
            Column.ColumnName = "AddDate";
            Column.PhysicalColumnName = "AddDate";
            Column.ColumnType = (int)Base.Enum.DMP.ColumnType.日期;
            Column.ColumnLenght = 0;
            Column.BindControl = (int)Base.Enum.DMP.ControlType.日期控件;
            Column.SortNum = 999;
            Column.SystemColumn = (int)Base.Enum.DMP.SystemColumn.系统字段;
            DB_Column.InsertOnlyInfo(Column);
            Column = new Frame_Column();
            Column.TableGuid = TableGuid;
            Column.ColumnName = "SortNum";
            Column.PhysicalColumnName = "SortNum";
            Column.ColumnType = (int)Base.Enum.DMP.ColumnType.整数;
            Column.ColumnLenght = 11;
            Column.ColumnDecimalPoint = 2;
            Column.BindControl = (int)Base.Enum.DMP.ControlType.数字框;
            Column.SortNum = 999;
            Column.SystemColumn = (int)Base.Enum.DMP.SystemColumn.系统字段;
            DB_Column.InsertOnlyInfo(Column);
        }

        public void InitColumnDataBaseStruct(Frame_Column Entity)
        {
            //创建列
            Frame_Table Table = DB_Table.SelectOne(Entity.TableGuid);
            string ColumnDDLSQL = string.Empty;
            switch (Entity.ColumnType)
            {
                case (int)Base.Enum.DMP.ColumnType.字符串:
                    ColumnDDLSQL = string.Format("{0}({1})", "nvarchar", Entity.ColumnLenght);
                    break;
                case (int)Base.Enum.DMP.ColumnType.小数:
                    ColumnDDLSQL = string.Format("{0}(10,{1})", "numeric", Entity.ColumnDecimalPoint);
                    break;
                case (int)Base.Enum.DMP.ColumnType.整数:
                    ColumnDDLSQL = string.Format("{0}({1})", "int", Entity.ColumnLenght);
                    break;
                case (int)Base.Enum.DMP.ColumnType.日期:
                    ColumnDDLSQL = string.Format("{0}({1})", "datetime", Entity.ColumnLenght);
                    break;
                case (int)Base.Enum.DMP.ColumnType.大文本:
                    ColumnDDLSQL = string.Format("{0}", "longtext", Entity.ColumnLenght);
                    break;
            }
            string FinalDDLSQL = string.Format("ALTER TABLE {0} ADD {1} {2}",
                  Table.TablePhysicalName, Entity.PhysicalColumnName, ColumnDDLSQL);
            DTO.ExecuteSQL(FinalDDLSQL);
        }

        public void DestoryColumnDataBaseStruct(string ColumnGuid)
        {
            Frame_Column Column = DB_Column.SelectOne(ColumnGuid);
            Frame_Table Table = DB_Table.SelectOne(Column.TableGuid);
            string DDLSQL = string.Format("ALTER TABLE {0} DROP COLUMN {1}",
                Table.TablePhysicalName, Column.PhysicalColumnName);
            DTO.ExecuteSQL(DDLSQL);
        }
    }
}
